package com.cloudfun.msglisten.service.impl;

import com.cloudfun.msglisten.common.constant.SysDictType;
import com.cloudfun.msglisten.entity.SystemDictionaryEntity;
import com.cloudfun.msglisten.repo.SystemDictionaryEntityRepo;
import com.cloudfun.msglisten.service.SysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by Huqin on 2020/2/6
 */
@Service
public class SysDictServiceImpl implements SysDictService {

    @Autowired
    private SystemDictionaryEntityRepo systemDictionaryEntityRepo;

    @Override
    //@Cacheable(cacheNames = CACHE_KEY, key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#root.args")
    public SystemDictionaryEntity getByTypeAndKey(SysDictType type, String key) {
        return systemDictionaryEntityRepo.findByTypeAndKey(type, key);
    }

    @Override
    //@Cacheable(cacheNames = CACHE_KEY, key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#root.args")
    public Map<String, String> getDictByType(SysDictType type) {
        List<SystemDictionaryEntity> dictionaryEntities = systemDictionaryEntityRepo.findAllByType(type);
        return dictionaryEntities.stream().collect(Collectors.toMap(SystemDictionaryEntity::getKey, SystemDictionaryEntity::getValue));
    }

}
